package com.idanchuang.component.core.config;


import com.idanchuang.component.core.helper.EnvContext;
import com.idanchuang.component.core.json.Jackson;
import org.springframework.beans.factory.InitializingBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author yjy
 * @date 2020/08/31 15:39
 **/
public class EnvConfigBean implements InitializingBean {

    /** 默认的 redis压测数据保留时间 */
    public static final long DEFAULT_PRESSURE_REDIS_EXPIRE = 7200L;

    /** 是否禁用压测环境逻辑 */
    private boolean disabled = true;
    /** 请求头名称 */
    private String headerName = "DC-ENV";
    /** 前端traceId的header名 */
    private String dcTraceIdHeaderName = "DC-TRACE-ID";
    /** 压测环境下的请求头值 */
    private String pressureValue = "pressure";
    /** 是否禁用压测环境下的逻辑-忽略日志 */
    private boolean disabledLog = true;
    /** 是否禁用压测环境下的逻辑-mysql影子表 */
    private boolean disabledMysql = true;
    /** 要忽略的表, 这些表将不会走影子表, 多个使用英文逗号分隔 */
    private String ignoreTables;
    /** 压测涉及到的数据库表, 多个表使用英文逗号分隔 */
    private String pressureTables;
    /** 是否禁用压测环境下的逻辑-redis */
    private boolean disabledRedis = true;
    /** 是否禁用压测环境下的逻辑-注解逻辑 */
    private boolean disabledAnnotation = true;
    /** redis 压测数据保留时间, 单位: 秒 */
    private Long pressureRedisExpire = DEFAULT_PRESSURE_REDIS_EXPIRE;

    @Override
    public void afterPropertiesSet() throws Exception {
        EnvContext.setEnvConfigBean(this);
    }

    public List<String> getIgnoreTableList() {
        List<String> list = new ArrayList<>();
        if (this.ignoreTables == null || "".equals(this.ignoreTables)) {
            return list;
        }
        list.addAll(Arrays.asList(this.ignoreTables.replace(" ", "").split(",")));
        return list;
    }

    public List<String> getPressureTableList() {
        List<String> list = new ArrayList<>();
        if (this.pressureTables == null || "".equals(this.pressureTables)) {
            return list;
        }
        list.addAll(Arrays.asList(this.pressureTables.replace(" ", "").split(",")));
        return list;
    }

    public String getPressureTables() {
        return pressureTables;
    }

    public void setPressureTables(String pressureTables) {
        this.pressureTables = pressureTables;
    }

    public String getIgnoreTables() {
        return ignoreTables;
    }

    public void setIgnoreTables(String ignoreTables) {
        this.ignoreTables = ignoreTables;
    }

    public String getHeaderName() {
        return headerName;
    }

    public void setHeaderName(String headerName) {
        this.headerName = headerName;
    }

    public String getPressureValue() {
        return pressureValue;
    }

    public void setPressureValue(String pressureValue) {
        this.pressureValue = pressureValue;
    }

    public String getDcTraceIdHeaderName() {
        return dcTraceIdHeaderName;
    }

    public boolean isDisabledRedis() {
        return disabledRedis;
    }

    public void setDisabledRedis(boolean disabledRedis) {
        this.disabledRedis = disabledRedis;
    }

    public void setDcTraceIdHeaderName(String dcTraceIdHeaderName) {
        this.dcTraceIdHeaderName = dcTraceIdHeaderName;
    }

    public Long getPressureRedisExpire() {
        return pressureRedisExpire;
    }

    public void setPressureRedisExpire(Long pressureRedisExpire) {
        this.pressureRedisExpire = pressureRedisExpire;
    }

    public boolean isDisabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public boolean isDisabledLog() {
        return disabledLog;
    }

    public void setDisabledLog(boolean disabledLog) {
        this.disabledLog = disabledLog;
    }

    public boolean isDisabledMysql() {
        return disabledMysql;
    }

    public void setDisabledMysql(boolean disabledMysql) {
        this.disabledMysql = disabledMysql;
    }

    public boolean isDisabledAnnotation() {
        return disabledAnnotation;
    }

    public void setDisabledAnnotation(boolean disabledAnnotation) {
        this.disabledAnnotation = disabledAnnotation;
    }

    @Override
    public String toString() {
        return "EnvConfigBean{" +
                "disabled=" + disabled +
                ", headerName='" + headerName + '\'' +
                ", dcTraceIdHeaderName='" + dcTraceIdHeaderName + '\'' +
                ", pressureValue='" + pressureValue + '\'' +
                ", disabledLog=" + disabledLog +
                ", disabledMysql=" + disabledMysql +
                ", ignoreTables='" + ignoreTables + '\'' +
                ", pressureTables='" + pressureTables + '\'' +
                ", disabledRedis=" + disabledRedis +
                ", disabledAnnotation=" + disabledAnnotation +
                ", pressureRedisExpire=" + pressureRedisExpire +
                '}';
    }
}
